package com.liang.interview.server.config.interceptor;

import cn.hutool.core.util.StrUtil;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * Feign拦截器
 */
@Component
public class FeignRequestInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
        ServletRequestAttributes request = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String loginId = null;
        if (request != null) {
            loginId = request.getRequest().getHeader("loginId");
        }
        if (StrUtil.isNotBlank(loginId)) {
            requestTemplate.header("loginId", loginId);
        }
    }
}
